package com.zhongpengcheng.blog.decorator;

import cn.hutool.extra.cglib.CglibUtil;
import com.zhongpengcheng.blog.dao.pojo.db.BlogDO;
import com.zhongpengcheng.blog.pojo.dto.BlogDTO;
import com.zhongpengcheng.blog.pojo.po.BlogPO;
import lombok.*;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * 博客实体装饰器
 * @author ZhongPengCheng
 * @version 1.0
 * @date 2021-11-25 20:33:00
 */
public class BlogDecorator {

    public static DOListDecorator of(List<BlogDO> sources) {
        return DOListDecorator.of().setSources(sources);
    }

    public static DODecorator of(BlogDO source) {
        return DODecorator.of().setSource(source);
    }

    public static PODecorator of(BlogPO source) {
        return PODecorator.of().setSource(source);
    }

    @Data(staticConstructor = "of")
    @Accessors(chain = true)
    public static class DOListDecorator {
        private List<BlogDO> sources;

        public List<BlogDTO> toDTOList() {
            return CglibUtil.copyList(sources, BlogDTO::new);
        }
    }

    @Data(staticConstructor = "of")
    @Accessors(chain = true)
    public static class DODecorator {
        private BlogDO source;

        public BlogDTO toDTO() {
            return CglibUtil.copy(source, BlogDTO.class);
        }
    }

    @Data(staticConstructor = "of")
    @Accessors(chain = true)
    public static class PODecorator {
        private BlogPO source;

        public BlogDO toDO() {
            return CglibUtil.copy(source, BlogDO.class);
        }
    }
}
